<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新行</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删行</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-print"
          size="mini"
          @click="handlePrint"
        >打印</el-button>
      </el-col>
<!--	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    </el-row>

    <el-table v-loading="loading" :data="dispatchListsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="" align="center" prop="autoid"   v-if="false" />
      <el-table-column label="" align="center" prop="dlid"  v-if="false" />
      <el-table-column label="" align="center" prop="icorid"  v-if="false" />
      <el-table-column label="" align="center" prop="cwhcode"  v-if="false"/>
      <el-table-column label="产品编码" align="center" prop="cinvcode.cinvcode" >
        <template #default="scope">
          <div style="position: relative; display: flex; align-items: flex-end;">
            <el-input  :value="scope.row.cinvcode && scope.row.cinvcode.cinvcode"   size="small"   style="width: 120px;"  readonly/>
            <el-button     icon="el-icon-search"  size="small"      style="position: absolute; right: 0; bottom: 0; padding: 2px 4px;"
                           @click="openProductDialog(scope.row,scope.$index)"
                           :aria-label="'Search product for ' + (scope.row.cinvcode && scope.row.cinvcode.cinvcode || '')"
                           title="Search product"
            ></el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="产品名称" align="center" prop="cinvcode.cinvname" >
        <template #default="scope">
          {{ scope.row.cinvcode && scope.row.cinvcode.cinvname || '' }}
        </template>
      </el-table-column>
      <el-table-column label="规格型号" align="center" prop="cinvcode.cinvstd" >
        <template #default="scope">
          {{ scope.row.cinvcode && scope.row.cinvcode.cinvstd || '' }}
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="iquantity" >
        <template #default="scope">
          <div class="order-action-row">
            <el-button
              size="small"
              class="order-btn"
              @click="decrease(scope.row, scope.$index)"
              :disabled="!scope.row.iquantity || scope.row.iquantity <= 1"
              icon="el-icon-minus"
            ></el-button>
            <el-input-number
              v-model="scope.row.iquantity"
              :min="0"
              :step="1"
              :controls="false"
              style="width:60px;margin-right:0px;"
              @change="handleOrderNumChange(scope.row, scope.$index)"
            />
            <el-button
              size="small"
              class="order-btn"
              @click="increase(scope.row,scope.$index)"
              :disabled="!scope.row.cinvcode.num || scope.row.iquantity >= scope.row.cinvcode.num"
              icon="el-icon-plus"
            ></el-button>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="inum"  v-if="false"/>
      <el-table-column label="报价" align="center" prop="iquotedprice" />
      <el-table-column label="折扣" align="center" prop="kl" />
      <el-table-column label="" align="center" prop="iunitprice"  v-if="false"/>
      <el-table-column label="含税价" align="center" prop="itaxunitprice" />
      <el-table-column label="无税金额" align="center" prop="imoney" />
      <el-table-column label="税额" align="center" prop="itax" />
      <el-table-column label="价税合计" align="center" prop="isum" />
      <el-table-column label="" align="center" prop="idiscount" v-if="false" />
      <el-table-column label="" align="center" prop="inatunitprice" v-if="false"/>
      <el-table-column label="" align="center" prop="inatmoney" v-if="false"/>
      <el-table-column label="" align="center" prop="inattax" v-if="false"/>
      <el-table-column label="" align="center" prop="inatsum" v-if="false"/>
      <el-table-column label="" align="center" prop="inatdiscount" v-if="false"/>
      <el-table-column label="" align="center" prop="isettlenum" v-if="false"/>
      <el-table-column label="" align="center" prop="isettlequantity" v-if="false"/>
      <el-table-column label="" align="center" prop="ibatch" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatch" v-if="false"/>
      <el-table-column label="" align="center" prop="bsettleall"  v-if="false" />
      <el-table-column label="备注" align="center" prop="cmemo" />
      <el-table-column label="" align="center" prop="cfree1"  v-if="false" />
      <el-table-column label="" align="center" prop="cfree2" v-if="false"/>
      <el-table-column label="" align="center" prop="itb"  v-if="false"/>
      <el-table-column label="" align="center" prop="dvdate" width="180" v-if="false" />
      <el-table-column label="" align="center" prop="tbquantity"  v-if="false"/>
      <el-table-column label="" align="center" prop="tbnum" v-if="false"/>
      <el-table-column label="" align="center" prop="isosid" v-if="false"/>
      <el-table-column label="" align="center" prop="idlsid" v-if="false"/>
      <el-table-column label="" align="center" prop="kl2" v-if="false"/>
      <el-table-column label="税率" align="center" prop="itaxrate" />
      <el-table-column label="" align="center" prop="cdefine22"   v-if="false" />
      <el-table-column label="" align="center" prop="cdefine23"   v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine24"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine25"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine26"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine27"  v-if="false"/>
      <el-table-column label="" align="center" prop="foutquantity"  v-if="false"/>
      <el-table-column label="" align="center" prop="foutnum"  v-if="false"/>
      <el-table-column label="" align="center" prop="citemcode"  v-if="false"/>
      <el-table-column label="" align="center" prop="citemClass"  v-if="false"/>
      <el-table-column label="" align="center" prop="fsalecost"  v-if="false"/>
      <el-table-column label="" align="center" prop="fsaleprice"  v-if="false"/>
      <el-table-column label="" align="center" prop="cvenabbname"  v-if="false"/>
      <el-table-column label="" align="center" prop="citemname"  v-if="false"/>
      <el-table-column label="" align="center" prop="citemCname"  v-if="false"/>
      <el-table-column label="" align="center" prop="cfree3"  v-if="false"/>
      <el-table-column label="" align="center" prop="cfree4"  v-if="false"/>
      <el-table-column label="" align="center" prop="cfree5"  v-if="false"/>
      <el-table-column label="" align="center" prop="cfree6"  v-if="false"/>
      <el-table-column label="" align="center" prop="cfree7"  v-if="false"/>
      <el-table-column label="" align="center" prop="cfree8"  v-if="false"/>
      <el-table-column label="" align="center" prop="cfree9"  v-if="false"/>
      <el-table-column label="" align="center" prop="cfree10"  v-if="false"/>
      <el-table-column label="" align="center" prop="bisstqc"  v-if="false"/>
      <el-table-column label="" align="center" prop="iinvexchrate"  v-if="false" />
      <el-table-column label="" align="center" prop="cunitid"   v-if="false"/>
      <el-table-column label="" align="center" prop="ccode"  v-if="false"/>
      <el-table-column label="" align="center" prop="iretquantity"  v-if="false"/>
      <el-table-column label="" align="center" prop="fensettlequan"  v-if="false"/>
      <el-table-column label="" align="center" prop="fensettlesum"  v-if="false"/>
      <el-table-column label="" align="center" prop="isettleprice"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine28"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine29"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine30"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine31"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine32"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine33"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine34"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine35"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine36"  v-if="false"/>
      <el-table-column label="" align="center" prop="cdefine37" v-if="false" />
      <el-table-column label="" align="center" prop="dmdate" v-if="false" />
      <el-table-column label="" align="center" prop="bgsp" v-if="false"/>
      <el-table-column label="" align="center" prop="cgspstate" v-if="false"/>
      <el-table-column label="" align="center" prop="csocode" v-if="false" />
      <el-table-column label="" align="center" prop="ccorcode" v-if="false" />
      <el-table-column label="" align="center" prop="ippartseqid"  v-if="false" />
      <el-table-column label="" align="center" prop="ippartid"  v-if="false" />
      <el-table-column label="" align="center" prop="ippartqty"  v-if="false" />
      <el-table-column label="" align="center" prop="ccontractid" v-if="false"/>
      <el-table-column label="" align="center" prop="ccontracttagcode" v-if="false"/>
      <el-table-column label="" align="center" prop="ccontractrowguid" v-if="false"/>
      <el-table-column label="" align="center" prop="imassdate" v-if="false"/>
      <el-table-column label="" align="center" prop="cmassunit" v-if="false"/>
      <el-table-column label="" align="center" prop="bqaneedcheck" v-if="false" />
      <el-table-column label="" align="center" prop="bqaurgency" v-if="false"/>
      <el-table-column label="" align="center" prop="bqachecking" v-if="false"/>
      <el-table-column label="" align="center" prop="bqachecked" v-if="false"/>
      <el-table-column label="" align="center" prop="iqaquantity" v-if="false"/>
      <el-table-column label="" align="center" prop="iqanum" v-if="false"/>
      <el-table-column label="" align="center" prop="ccusinvcode"  v-if="false" />
      <el-table-column label="" align="center" prop="ccusinvname" v-if="false"/>
      <el-table-column label="" align="center" prop="fsumsignquantity" v-if="false"/>
      <el-table-column label="" align="center" prop="fsumsignnum" v-if="false"/>
      <el-table-column label="" align="center" prop="cbaccounter"  v-if="false" />
      <el-table-column label="" align="center" prop="bcosting" v-if="false"/>
      <el-table-column label="" align="center" prop="cordercode"  v-if="false" />
      <el-table-column label="" align="center" prop="iorderrowno" v-if="false"/>
      <el-table-column label="" align="center" prop="fcusminprice" v-if="false"/>
      <el-table-column label="" align="center" prop="icostquantity" v-if="false"/>
      <el-table-column label="" align="center" prop="icostsum" v-if="false"/>
      <el-table-column label="" align="center" prop="ispecialtype" v-if="false"/>
      <el-table-column label="" align="center" prop="cvmivencode" v-if="false"/>
      <el-table-column label="" align="center" prop="iexchsum" v-if="false"/>
      <el-table-column label="" align="center" prop="imoneysum" v-if="false"/>
      <el-table-column label="" align="center" prop="irowno" v-if="false"/>
      <el-table-column label="" align="center" prop="frettbquantity" v-if="false"/>
      <el-table-column label="" align="center" prop="fretsum" v-if="false"/>
      <el-table-column label="" align="center" prop="iexpiratdatecalcu" v-if="false"/>
      <el-table-column label="" align="center" prop="dexpirationdate" v-if="false" />
      <el-table-column label="" align="center" prop="cexpirationdate" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty1" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty2" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty3" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty4" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty5" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty6" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty7" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty8" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty9" v-if="false"/>
      <el-table-column label="" align="center" prop="cbatchproperty10" v-if="false" />
      <el-table-column label="" align="center" prop="dblpreexchmomey" v-if="false"/>
      <el-table-column label="" align="center" prop="dblpremomey" v-if="false"/>
      <el-table-column label="" align="center" prop="idemandtype" v-if="false"/>
      <el-table-column label="" align="center" prop="cdemandcode" v-if="false"/>
      <el-table-column label="" align="center" prop="cdemandmemo" v-if="false"/>
      <el-table-column label="" align="center" prop="cdemandid" v-if="false"/>
      <el-table-column label="" align="center" prop="idemandseq" v-if="false"/>
      <el-table-column label="" align="center" prop="cvencode" v-if="false"/>
      <el-table-column label="" align="center" prop="creasoncode" v-if="false"/>
      <el-table-column label="" align="center" prop="cinvsn" v-if="false"/>
      <el-table-column label="" align="center" prop="iinvsncount" v-if="false"/>
      <el-table-column label="" align="center" prop="bneedsign" v-if="false"/>
      <el-table-column label="" align="center" prop="bsignover" v-if="false"/>
      <el-table-column label="" align="center" prop="bneedloss" v-if="false"/>
      <el-table-column label="" align="center" prop="flossrate" v-if="false"/>
      <el-table-column label="" align="center" prop="frlossqty" v-if="false"/>
      <el-table-column label="" align="center" prop="fulossqty" v-if="false"/>
      <el-table-column label="" align="center" prop="isettletype" v-if="false"/>
      <el-table-column label="" align="center" prop="crelacuscode" v-if="false"/>
      <el-table-column label="" align="center" prop="clossmaker" v-if="false"/>
      <el-table-column label="" align="center" prop="dlossdate" v-if="false" />
      <el-table-column label="" align="center" prop="dlosstime" v-if="false" />
      <el-table-column label="" align="center" prop="icoridlsid" v-if="false"/>
      <el-table-column label="" align="center" prop="fretoutqty" v-if="false"/>
      <el-table-column label="" align="center" prop="bodyOutid" v-if="false"/>
      <el-table-column label="" align="center" prop="fveribillqty" v-if="false"/>
      <el-table-column label="" align="center" prop="fveribillsum" v-if="false"/>
      <el-table-column label="" align="center" prop="fveriretqty" v-if="false"/>
      <el-table-column label="" align="center" prop="fveriretsum" v-if="false"/>
      <el-table-column label="" align="center" prop="flastsettleqty" v-if="false"/>
      <el-table-column label="" align="center" prop="flastsettlesum" v-if="false"/>
      <el-table-column label="" align="center" prop="cbookwhcode" v-if="false"/>
      <el-table-column label="" align="center" prop="cinvouchtype" v-if="false"/>
      <el-table-column label="" align="center" prop="cposition" v-if="false" />
      <el-table-column label="" align="center" prop="fretqtywkp" v-if="false" />
      <el-table-column label="" align="center" prop="fretqtyykp" v-if="false"/>
      <el-table-column label="" align="center" prop="frettbqtyykp" v-if="false"/>
      <el-table-column label="" align="center" prop="fretsumykp" v-if="false" />
      <el-table-column label="" align="center" prop="dkeepdate" v-if="false" />
      <el-table-column label="" align="center" prop="cscloser"  v-if="false"/>
      <el-table-column label="" align="center" prop="isaleoutid" v-if="false"/>
      <el-table-column label="" align="center" prop="bsaleprice" v-if="false"/>
      <el-table-column label="" align="center" prop="bgift" v-if="false"/>
      <el-table-column label="" align="center" prop="bmpforderclosed" v-if="false"/>
      <el-table-column label="" align="center" prop="cbsysbarcode"  v-if="false" />
      <el-table-column label="" align="center" prop="fxjquantity" v-if="false" />
      <el-table-column label="" align="center" prop="fxjnum" v-if="false" />
      <el-table-column label="" align="center" prop="biacreatebill" v-if="false" />
      <el-table-column label="" align="center" prop="cparentcode" v-if="false" />
      <el-table-column label="" align="center" prop="cchildcode" v-if="false" />
      <el-table-column label="" align="center" prop="fchildqty" v-if="false" />
      <el-table-column label="" align="center" prop="fchildrate" v-if="false" />
      <el-table-column label="" align="center" prop="icalctype" v-if="false" />
      <el-table-column label="" align="center" prop="fappretwkpqty" v-if="false" />
      <el-table-column label="" align="center" prop="fappretwkpsum" v-if="false"/>
      <el-table-column label="" align="center" prop="fappretykpqty" v-if="false"/>
      <el-table-column label="" align="center" prop="fappretykpsum" v-if="false"/>
      <el-table-column label="" align="center" prop="fappretwkptbqty" v-if="false"/>
      <el-table-column label="" align="center" prop="fappretykptbqty" v-if="false"/>
      <el-table-column label="" align="center" prop="irtnappid" v-if="false"/>
      <el-table-column label="" align="center" prop="crtnappcode" v-if="false"/>
      <el-table-column label="" align="center" prop="fretailrealamount" v-if="false"/>
      <el-table-column label="" align="center" prop="fretailsettleamount" v-if="false"/>
      <el-table-column label="" align="center" prop="cfactorycode" v-if="false"/>
      <el-table-column label="" align="center" prop="gcsourceid" v-if="false"/>
      <el-table-column label="" align="center" prop="gcsourceids" v-if="false"/>
      <el-table-column label="" align="center" prop="cconfirmer" v-if="false" />
      <el-table-column label="" align="center" prop="dconfirmdate" v-if="false" />
      <el-table-column label="" align="center" prop="dsclosedate" v-if="false" />
      <el-table-column label="" align="center" prop="dsclosesystime" v-if="false" />
    </el-table>

    <el-dialog      title="选择商品"      :visible.sync="productDialogVisible"     width="98%" >
      <Inventory    @rowDbclick="onProductSelect"    :isShowcar=false  />
    </el-dialog>

    <!-- 打印预览对话框 -->
    <el-dialog title="打印预览" :visible.sync="printDialogVisible" width="90%" :before-close="handlePrintDialogClose">
      <div class="print-preview">
        <div class="print-header">
          <h2>销售发货单</h2>
          <div class="print-info">
            <span>打印时间：{{ new Date().toLocaleString() }}</span>
            <span>打印人：{{ $store.state.user.name }}</span>
          </div>
        </div>

        <!-- 表头信息 -->
        <div class="print-header-info">
          <el-row :gutter="20">
            <el-col :span="6">
              <div class="info-item">
                <label>发货单号：</label>
                <span>{{ headerInfo.cdlcode }}</span>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="info-item">
                <label>日期：</label>
                <span>{{ headerInfo.ddate }}</span>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="info-item">
                <label>客户编码：</label>
                <span>{{ headerInfo.ccuscode }}</span>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="info-item">
                <label>课题号：</label>
                <span>{{ headerInfo.cdefine11 }}</span>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="info-item">
                <label>客户名称：</label>
                <span>{{ headerInfo.ccusname }}</span>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="info-item">
                <label>联系人：</label>
                <span>{{ headerInfo.ccusperson }}</span>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="info-item">
                <label>联系手机：</label>
                <span>{{ headerInfo.cmobilephone || '-' }}</span>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="12">
              <div class="info-item">
                <label>收货地址：</label>
                <span>{{ headerInfo.cshipaddress }}</span>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="info-item">
                <label>填制人：</label>
                <span>{{ headerInfo.cmodifier }}</span>
              </div>
            </el-col>
            <el-col :span="6">
              <div class="info-item">
                <label>审核人：</label>
                <span>{{ headerInfo.cverifier || '-' }}</span>
              </div>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="24">
              <div class="info-item">
                <label>备注：</label>
                <span>{{ headerInfo.cmemo || '-' }}</span>
              </div>
            </el-col>
          </el-row>
        </div>

        <div class="print-content">
          <el-table :data="printData" border stripe>
            <el-table-column label="序号" type="index" width="50" align="center" />
            <el-table-column label="产品编码" prop="cinvcode.cinvcode" align="center" />
            <el-table-column label="产品名称" prop="cinvcode.cinvname" align="center" />
            <el-table-column label="规格型号" prop="cinvcode.cinvstd" align="center" />
            <el-table-column label="数量" prop="iquantity" align="center" />
            <el-table-column label="报价" prop="iquotedprice" align="center" />
            <el-table-column label="折扣" prop="kl" align="center" />
            <el-table-column label="含税价" prop="itaxunitprice" align="center" />
            <el-table-column label="无税金额" prop="imoney" align="center" />
            <el-table-column label="税额" prop="itax" align="center" />
            <el-table-column label="价税合计" prop="isum" align="center" />
            <el-table-column label="税率" prop="itaxrate" align="center" />
            <el-table-column label="备注" prop="cmemo" align="center" />
          </el-table>
        </div>

        <div class="print-summary">
          <div class="summary-item">
            <span>总数量：{{ getTotalQuantity() }}</span>
            <span>总金额：{{ getTotalAmount() }}</span>
            <span>总税额：{{ getTotalTax() }}</span>
            <span>价税合计：{{ getTotalSum() }}</span>
          </div>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="printDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="doPrint">打印</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {  getDispatchLists, delDispatchLists, addDispatchLists, updateDispatchLists, exportDispatchLists } from "@/api/business/DispatchLists";
import Inventory      from  "@/views/business/Inventory/index.vue";
import {saveCar} from "@/api/business/Inventory";
import { PDFDocument, rgb, StandardFonts } from 'pdf-lib';

export default {
  name: "DispatchLists",
  components: {
    Inventory
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // DispatchLists表格数据
      dispatchListsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 1000,
        dlid: null,
      },
      irowno: 0,
      productDialogVisible: false,
      productSelectRow: null,
      index: null,
      // 打印相关
      printDialogVisible: false,
      printData: [],
      // 表头信息
      headerInfo: {},
    };
  },
  props:{
    isDialog: {
      type: Boolean,
      default: false,
    },  detailResponse:{
      type: Object,
      default: {}
    },
  },
  created() {
  //  this.getList();
  },
  beforeDestroy() {
    // 清理事件监听器和引用，避免组件销毁时的错误
    this.productSelectRow = null;
    this.dispatchListsList = [];
    this.index=null;
  },
  methods: {
    /** 查询DispatchLists列表 */
   setData() {
     debugger;
      this.dispatchListsList = this.detailResponse.rows || [];
      this.total = this.detailResponse.total || 0;
      this.irowno = this.dispatchListsList && this.dispatchListsList.length > 0
        ? this.dispatchListsList.map(item => item.irowno).reduce((a, b) => Math.max(a, b), 0)
        : 0;
      this.loading = false; // 数据设置完成后关闭loading
    },
    getData(){
      return this.dispatchListsList;
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    openProductDialog(row,index) {
      this.productSelectRow = row;
      this.productDialogVisible = true;
      this.index=index;
    },
    onProductSelect(product) {
      if (this.productSelectRow && product) {
      //  this.productSelectRow.dlid=this.dlid;
        this.productSelectRow.cinvcode.cinvcode = product.cinvcode;
        this.productSelectRow.cinvcode.cinvname = product.cinvname;
        this.productSelectRow.cinvcode.cinvstd = product.cinvstd;
        this.productSelectRow.cinvcode.num=product.num;
        this.productSelectRow.comunitname = product.comunitname;
        this.productSelectRow.cinvname=product.cinvname
        this.productSelectRow.itaxrate = product.itaxrate;
        this.productSelectRow.iquotedprice = product.saleprice;
        this.productSelectRow.kl = product.rate;
        this.productSelectRow.itaxunitprice = parseFloat(product.saleprice * product.rate / 100).toFixed(2);
        this.productSelectRow.iunitprice=parseFloat(this.productSelectRow.itaxunitprice/(parseFloat(this.productSelectRow.itaxrate)+100)*100).toFixed(6);
        this.productSelectRow.iquantity = product.orderNum;
        this.productSelectRow.inatunitprice= this.productSelectRow.iunitprice;
        this.productSelectRow.inatmoney= this.productSelectRow.imoney;
        this.productSelectRow.inattax= this.productSelectRow.itax;
        this.productSelectRow.inatsum= this.productSelectRow.isum;
        this.productSelectRow.num = product.num;
        this.productSelectRow.isum = parseFloat(this.productSelectRow.itaxunitprice * this.productSelectRow.iquantity).toFixed(2);
        //  row.amount=row.isum;
        this.productSelectRow.imoney = parseFloat(parseFloat(this.productSelectRow.isum)/(parseFloat(this.productSelectRow.itaxrate)+100)*100).toFixed(2);
        this.productSelectRow.itax=parseFloat(this.productSelectRow.isum-this.productSelectRow.imoney).toFixed(2);
        this.dispatchListsList[this.index] = this.productSelectRow;
      }
      this.productDialogVisible = false;
    },
    handleOrderNumChange(row,idx) {
     debugger;
      const price = parseFloat(row.itaxunitprice) || 0;
      const iquantity = parseFloat(row.iquantity) || 0;
      row.isum = (price * iquantity).toFixed(2); // 价税合计保留两位小数
      row.imoney= parseFloat(parseFloat(row.isum)/(parseFloat(row.itaxrate)+100)*100).toFixed(2);
      row.itax=parseFloat(row.isum-row.imoney).toFixed(2);
      this.dispatchListsList[idx]=row;
    },
    increase(row,idx) {
      if (row.iquantity < row.cinvcode.num) {
        row.iquantity++;
        this.handleOrderNumChange(row,idx);
      }
    },
    decrease(row,idx) {
      if (row.iquantity > 0) {
        row.iquantity--;
        this.handleOrderNumChange(row,idx);
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.irowno)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // 创建新的空行数据
      const newRow = {
        autoid: null, // 临时ID
        cinvcode: {
          cinvcode: '',
          cinvname: '',
          cinvstd: ''
        },
        iquantity: 1, // 默认数量为1
        iquotedprice: 0,
        kl: 100, // 默认折扣100%
        itaxunitprice: 0,
        num: 999999, // 默认库存数量很大
        itaxrate: 13, // 默认税率13%
        isum: 0,
        imoney: 0,
        itax: 0,
        cmemo: '',
        cwhcode:'bzp', // 默认仓库编码
        // 添加其他必要的字段
        comunitname: '',
        saleprice: 0,
        rate: 100,
        ordernum: 0,
        irowno: ++this.irowno,
      };
      // 将新行添加到表格数据中
      this.dispatchListsList.push(newRow);

      // 更新表格显示并自动聚焦到新行
      this.$nextTick(() => {
        // 滚动到新行
        const tableBody = this.$el.querySelector('.el-table__body-wrapper');
        if (tableBody) {
          tableBody.scrollTop = tableBody.scrollHeight;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除选中的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        // 删除选中行的数据
        if (this.ids && this.ids.length > 0) {
          // 删除多行
          console.log("ids:", this.ids);
          console.log("dispatchListsList:", this.dispatchListsList);
          this.dispatchListsList = this.dispatchListsList.filter(item => !this.ids.includes(item.irowno));
        }
        // 清空选择
        this.ids = [];
        this.single = true;
        this.multiple = true;
        this.$message.success("删除成功");
      }).catch(() => {
        // 用户取消删除
      });
    },
    handlePrint(row) {
      // 获取选中的数据
      // 显示打印预览
      this.printData = this.dispatchListsList;
      this.printDialogVisible = true;
    },
    // 打印预览对话框关闭前处理
    handlePrintDialogClose(done) {
      this.printData = []; // 清空打印数据
      done();
    },
    // 获取总数量
    getTotalQuantity() {
      return this.printData.reduce((sum, item) => sum + parseFloat(item.iquantity) || 0, 0);
    },
    // 获取总金额
    getTotalAmount() {
      return this.printData.reduce((sum, item) => sum + parseFloat(item.imoney) || 0, 0).toFixed(2);
    },
    // 获取总税额
    getTotalTax() {
      return this.printData.reduce((sum, item) => sum + parseFloat(item.itax) || 0, 0).toFixed(2);
    },
    // 获取总价税合计
    getTotalSum() {
      return this.printData.reduce((sum, item) => sum + parseFloat(item.isum) || 0, 0).toFixed(2);
    },
    // 打印
    doPrint() {
      if (this.printData.length === 0) {
        this.$message.warning('没有数据可打印');
        return;
      }

      // 创建打印内容
      const printContent = this.generatePrintContent();

      try {
        // 创建打印窗口
        const printWindow = window.open('', '_blank');

        // 检查窗口是否成功打开
        if (!printWindow) {
          this.$message.error('无法打开打印窗口，请检查浏览器设置');
          return;
        }

        printWindow.document.write(printContent);
        printWindow.document.close();

        // 等待内容加载完成后打印
        printWindow.onload = () => {
          try {
            printWindow.print();
            // 延迟关闭窗口，确保打印完成
            setTimeout(() => {
              printWindow.close();
            }, 1000);
          } catch (printError) {
            console.error('打印失败:', printError);
            this.$message.error('打印失败，请重试');
          }
        };

        this.$message.success('打印成功！');
        this.printDialogVisible = false;
      } catch (error) {
        console.error('打印出错:', error);
        this.$message.error('打印出错，请重试');
      }
    },
    // 导出PDF
    async exportToPDF() {
      if (this.dispatchListsList.length === 0) {
        this.$message.warning('没有数据可导出');
        return;
      }

      // 显示加载提示
      const loading = this.$loading({
        lock: true,
        text: '正在生成PDF文件...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });

         // 准备导出参数
        const exportParams = {
          cdlcode: this.headerInfo.cdlcode || '',
          ddate: this.headerInfo.ddate || '',
          ccuscode: this.headerInfo.ccuscode || '',
          ccusname: this.headerInfo.ccusname || '',
          ccusperson: this.headerInfo.ccusperson || '',
          cmobilephone: this.headerInfo.cmobilephone || '',
          cshipaddress: this.headerInfo.cshipaddress || '',
          cmodifier: this.headerInfo.cmodifier || '',
          cverifier: this.headerInfo.cverifier || '',
          cmemo: this.headerInfo.cmemo || '',
          cdefine11: this.headerInfo.cdefine11 || '',
          dispatchLists: this.dispatchListsList
        };

        // 调用后端API生成PDF
        const response = await this.$http.post('/business/dispatchLists/exportPDF', exportParams, {
          responseType: 'blob' // 设置响应类型为blob
        });

        // 创建下载链接
        const blob = new Blob([response.data], { type: 'application/pdf' });
        const url = URL.createObjectURL(blob);
        const link = document.createElement('a');
        link.href = url;
        link.download = `调度单明细表_${this.headerInfo.cdlcode || '未知单号'}_${new Date().toISOString().split('T')[0]}.pdf`;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        URL.revokeObjectURL(url);
        loading.close();
        this.$message.success('PDF导出成功！');


    },
    // 生成打印内容
    generatePrintContent() {
      const printTime = new Date().toLocaleString();
      const userName = this.$store.state.user.name || '未知用户';

      let tableRows = '';
      this.printData.forEach((item, index) => {
        tableRows += `
          <tr>
            <td>${index + 1}</td>
            <td>${item.cinvcode?.cinvcode || ''}</td>
            <td>${item.cinvcode?.cinvname || ''}</td>
            <td>${item.cinvcode?.cinvstd || ''}</td>
            <td>${item.iquantity || 0}</td>
            <td>${item.iquotedprice || 0}</td>
            <td>${item.kl || 100}</td>
            <td>${item.itaxunitprice || 0}</td>
            <td>${item.imoney || 0}</td>
            <td>${item.itax || 0}</td>
            <td>${item.isum || 0}</td>
            <td>${item.itaxrate || 0}</td>
            <td>${item.cmemo || ''}</td>
          </tr>
        `;
      });

      // 生成表头信息HTML
      const headerInfoHtml = `
        <div class="print-header-info">
          <table class="header-table">
            <tr>
              <td><strong>发货单号：</strong>${this.headerInfo.cdlcode || ''}</td>
              <td><strong>日期：</strong>${this.headerInfo.ddate || ''}</td>
              <td><strong>客户编码：</strong>${this.headerInfo.ccuscode || ''}</td>
              <td><strong>课题号：</strong>${this.headerInfo.cdefine11 || ''}</td>
            </tr>
            <tr>
              <td colspan="2"><strong>客户名称：</strong>${this.headerInfo.ccusname || ''}</td>
              <td><strong>联系人：</strong>${this.headerInfo.ccusperson || ''}</td>
              <td><strong>联系手机：</strong>${this.headerInfo.cmobilephone || '-'}</td>
            </tr>
            <tr>
              <td colspan="2"><strong>收货地址：</strong>${this.headerInfo.cshipaddress || ''}</td>
              <td><strong>填制人：</strong>${this.headerInfo.cmodifier || ''}</td>
              <td><strong>审核人：</strong>${this.headerInfo.cverifier || '-'}</td>
            </tr>
            <tr>
              <td colspan="4"><strong>备注：</strong>${this.headerInfo.cmemo || '-'}</td>
            </tr>
          </table>
        </div>
      `;

      return `
        <!DOCTYPE html>
        <html>
        <head>
          <meta charset="UTF-8">
          <title>调度单明细表</title>
          <style>
            body { font-family: Arial, sans-serif; margin: 20px; }
            .print-header { text-align: center; margin-bottom: 20px; }
            .print-header h2 { margin: 0; color: #333; }
            .print-info { margin: 10px 0; color: #666; }
            .print-info span { margin: 0 15px; }
            .print-header-info { margin: 20px 0; }
            .header-table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
            .header-table td { border: 1px solid #ddd; padding: 8px; text-align: left; }
            .header-table strong { color: #555; }
            table { width: 100%; border-collapse: collapse; margin: 20px 0; }
            th, td { border: 1px solid #ddd; padding: 8px; text-align: center; }
            th { background-color: #f5f5f5; font-weight: bold; }
            .print-summary { margin-top: 20px; text-align: right; }
            .summary-item span { margin: 0 15px; font-weight: bold; }
            @media print {
              body { margin: 0; }
              .no-print { display: none; }
            }
          </style>
        </head>
        <body>
          <div class="print-header">
            <h2>调度单明细表</h2>
            <div class="print-info">
              <span>打印时间：${printTime}</span>
              <span>打印人：${userName}</span>
            </div>
          </div>

          ${headerInfoHtml}

          <table>
            <thead>
              <tr>
                <th>序号</th>
                <th>产品编码</th>
                <th>产品名称</th>
                <th>规格型号</th>
                <th>数量</th>
                <th>报价</th>
                <th>折扣</th>
                <th>含税价</th>
                <th>无税金额</th>
                <th>税额</th>
                <th>价税合计</th>
                <th>税率</th>
                <th>备注</th>
              </tr>
            </thead>
            <tbody>
              ${tableRows}
            </tbody>
          </table>

          <div class="print-summary">
            <div class="summary-item">
              <span>总数量：${this.getTotalQuantity()}</span>
              <span>总金额：${this.getTotalAmount()}</span>
              <span>总税额：${this.getTotalTax()}</span>
              <span>价税合计：${this.getTotalSum()}</span>
            </div>
          </div>
        </body>
        </html>
      `;
    },clearData(){
      this.dispatchListsList=[];
    }
  }
};
</script>

<style scoped>
.order-action-row {
  display: flex;
  align-items: center;
  justify-content: center;
}
.order-btn {
  min-width: 28px;
  height: 28px;
  padding: 0;
  margin: 0 2px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 打印预览样式 */
.print-preview {
  padding: 20px;
}

.print-header {
  text-align: center;
  margin-bottom: 20px;
  border-bottom: 2px solid #409EFF;
  padding-bottom: 15px;
}

.print-header h2 {
  margin: 0;
  color: #333;
  font-size: 24px;
}

.print-info {
  margin: 15px 0;
  color: #666;
  font-size: 14px;
}

.print-info span {
  margin: 0 20px;
}

.print-header-info {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f5f5f5;
  border-radius: 5px;
}

.info-item {
  margin-bottom: 10px;
}

.info-item label {
  font-weight: bold;
  margin-right: 10px;
  color: #555;
}

.info-item span {
  color: #333;
  font-size: 15px;
}

/* 表头信息表格样式 */
.header-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

.header-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
  background-color: #f9f9f9;
}

.header-table strong {
  color: #555;
  font-weight: bold;
}

.print-content {
  margin: 20px 0;
}

.print-summary {
  margin-top: 30px;
  text-align: right;
  border-top: 1px solid #ddd;
  padding-top: 15px;
}

.summary-item span {
  margin: 0 20px;
  font-weight: bold;
  color: #409EFF;
  font-size: 16px;
}

/* 打印样式 */
@media print {
  .print-preview {
    padding: 0;
  }

  .print-header {
    border-bottom: 2px solid #000;
  }

  .print-summary {
    border-top: 1px solid #000;
  }
}
</style>
